package net.gz01.wallet.web.controller.api.sdk;//package net.gz01.wallet.web.controller.api.sdk;
//
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.config.RequestConfig;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.config.RegistryBuilder;
//import org.apache.http.conn.ConnectTimeoutException;
//import org.apache.http.conn.socket.ConnectionSocketFactory;
//import org.apache.http.conn.socket.PlainConnectionSocketFactory;
//import org.apache.http.conn.ssl.DefaultHostnameVerifier;
//import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.HttpClientBuilder;
//import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
//import org.apache.http.util.EntityUtils;
//
//import javax.net.ssl.KeyManagerFactory;
//import javax.net.ssl.SSLContext;
//import java.io.InputStream;
//import java.net.SocketTimeoutException;
//import java.net.UnknownHostException;
//import java.security.KeyStore;
//import java.security.SecureRandom;
//
//import static net.gz01.wallet.web.controller.api.sdk.WXPayConstants.USER_AGENT;
//
//public class WXPayRequest {
//    private WXPayConfig config;
//    public WXPayRequest(WXPayConfig config) throws Exception{
//
//        this.config = config;
//    }
//
//    /**
//     * 璇锋眰锛屽彧璇锋眰涓�娆★紝涓嶅仛閲嶈瘯
//     * @param domain
//     * @param urlSuffix
//     * @param uuid
//     * @param data
//     * @param connectTimeoutMs
//     * @param readTimeoutMs
//     * @param useCert 鏄惁浣跨敤璇佷功锛岄拡瀵归��娆俱�佹挙閿�绛夋搷浣�
//     * @return
//     * @throws Exception
//     */
//    private String requestOnce(final String domain, String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean useCert) throws Exception {
//        BasicHttpClientConnectionManager connManager;
//        if (useCert) {
//            // 璇佷功
//            char[] password = config.getMchID().toCharArray();
//            InputStream certStream = config.getCertStream();
//            KeyStore ks = KeyStore.getInstance("PKCS12");
//            ks.load(certStream, password);
//
//            // 瀹炰緥鍖栧瘑閽ュ簱 & 鍒濆鍖栧瘑閽ュ伐鍘�
//            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
//            kmf.init(ks, password);
//
//            // 鍒涘缓 SSLContext
//            SSLContext sslContext = SSLContext.getInstance("TLS");
//            sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());
//
//            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
//                    sslContext,
//                    new String[]{"TLSv1"},
//                    null,
//                    new DefaultHostnameVerifier());
//
//            connManager = new BasicHttpClientConnectionManager(
//                    RegistryBuilder.<ConnectionSocketFactory>create()
//                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
//                            .register("https", sslConnectionSocketFactory)
//                            .build(),
//                    null,
//                    null,
//                    null
//            );
//        }
//        else {
//            connManager = new BasicHttpClientConnectionManager(
//                    RegistryBuilder.<ConnectionSocketFactory>create()
//                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
//                            .register("https", SSLConnectionSocketFactory.getSocketFactory())
//                            .build(),
//                    null,
//                    null,
//                    null
//            );
//        }
//
//        HttpClient httpClient = HttpClientBuilder.create()
//                .setConnectionManager(connManager)
//                .build();
//
//        String url = "https://" + domain + urlSuffix;
//        HttpPost httpPost = new HttpPost(url);
//
//        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
//        httpPost.setConfig(requestConfig);
//
//        StringEntity postEntity = new StringEntity(data, "UTF-8");
//        httpPost.addHeader("Content-Type", "text/xml");
//        httpPost.addHeader("User-Agent", USER_AGENT + " " + config.getMchID());
//        httpPost.setEntity(postEntity);
//
//        HttpResponse httpResponse = httpClient.execute(httpPost);
//        HttpEntity httpEntity = httpResponse.getEntity();
//        return EntityUtils.toString(httpEntity, "UTF-8");
//
//    }
//
//
//    private String request(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean useCert, boolean autoReport) throws Exception {
//        Exception exception = null;
//        long elapsedTimeMillis = 0;
//        long startTimestampMs = WXPayUtil.getCurrentTimestampMs();
//        boolean firstHasDnsErr = false;
//        boolean firstHasConnectTimeout = false;
//        boolean firstHasReadTimeout = false;
//        IWXPayDomain.DomainInfo domainInfo = config.getWXPayDomain().getDomain(config);
//        if(domainInfo == null){
//            throw new Exception("WXPayConfig.getWXPayDomain().getDomain() is empty or null");
//        }
//        try {
//            String result = requestOnce(domainInfo.domain, urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, useCert);
//            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
//            config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, null);
//            WXPayReport.getInstance(config).report(
//                    uuid,
//                    elapsedTimeMillis,
//                    domainInfo.domain,
//                    domainInfo.primaryDomain,
//                    connectTimeoutMs,
//                    readTimeoutMs,
//                    firstHasDnsErr,
//                    firstHasConnectTimeout,
//                    firstHasReadTimeout);
//            return result;
//        }
//        catch (UnknownHostException ex) {  // dns 瑙ｆ瀽閿欒锛屾垨鍩熷悕涓嶅瓨鍦�
//            exception = ex;
//            firstHasDnsErr = true;
//            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
//            WXPayUtil.getLogger().warn("UnknownHostException for domainInfo {}", domainInfo);
//            WXPayReport.getInstance(config).report(
//                    uuid,
//                    elapsedTimeMillis,
//                    domainInfo.domain,
//                    domainInfo.primaryDomain,
//                    connectTimeoutMs,
//                    readTimeoutMs,
//                    firstHasDnsErr,
//                    firstHasConnectTimeout,
//                    firstHasReadTimeout
//            );
//        }
//        catch (ConnectTimeoutException ex) {
//            exception = ex;
//            firstHasConnectTimeout = true;
//            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
//            WXPayUtil.getLogger().warn("connect timeout happened for domainInfo {}", domainInfo);
//            WXPayReport.getInstance(config).report(
//                    uuid,
//                    elapsedTimeMillis,
//                    domainInfo.domain,
//                    domainInfo.primaryDomain,
//                    connectTimeoutMs,
//                    readTimeoutMs,
//                    firstHasDnsErr,
//                    firstHasConnectTimeout,
//                    firstHasReadTimeout
//            );
//        }
//        catch (SocketTimeoutException ex) {
//            exception = ex;
//            firstHasReadTimeout = true;
//            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
//            WXPayUtil.getLogger().warn("timeout happened for domainInfo {}", domainInfo);
//            WXPayReport.getInstance(config).report(
//                    uuid,
//                    elapsedTimeMillis,
//                    domainInfo.domain,
//                    domainInfo.primaryDomain,
//                    connectTimeoutMs,
//                    readTimeoutMs,
//                    firstHasDnsErr,
//                    firstHasConnectTimeout,
//                    firstHasReadTimeout);
//        }
//        catch (Exception ex) {
//            exception = ex;
//            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs()-startTimestampMs;
//            WXPayReport.getInstance(config).report(
//                    uuid,
//                    elapsedTimeMillis,
//                    domainInfo.domain,
//                    domainInfo.primaryDomain,
//                    connectTimeoutMs,
//                    readTimeoutMs,
//                    firstHasDnsErr,
//                    firstHasConnectTimeout,
//                    firstHasReadTimeout);
//        }
//        config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, exception);
//        throw exception;
//    }
//
//
//    /**
//     * 鍙噸璇曠殑锛岄潪鍙屽悜璁よ瘉鐨勮姹�
//     * @param urlSuffix
//     * @param uuid
//     * @param data
//     * @return
//     */
//    public String requestWithoutCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception {
//        return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), false, autoReport);
//    }
//
//    /**
//     * 鍙噸璇曠殑锛岄潪鍙屽悜璁よ瘉鐨勮姹�
//     * @param urlSuffix
//     * @param uuid
//     * @param data
//     * @param connectTimeoutMs
//     * @param readTimeoutMs
//     * @return
//     */
//    public String requestWithoutCert(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs,  boolean autoReport) throws Exception {
//        return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, false, autoReport);
//    }
//
//    /**
//     * 鍙噸璇曠殑锛屽弻鍚戣璇佺殑璇锋眰
//     * @param urlSuffix
//     * @param uuid
//     * @param data
//     * @return
//     */
//    public String requestWithCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception {
//        return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), true, autoReport);
//    }
//
//    /**
//     * 鍙噸璇曠殑锛屽弻鍚戣璇佺殑璇锋眰
//     * @param urlSuffix
//     * @param uuid
//     * @param data
//     * @param connectTimeoutMs
//     * @param readTimeoutMs
//     * @return
//     */
//    public String requestWithCert(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs, boolean autoReport) throws Exception {
//        return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, true, autoReport);
//    }
//}
